<?php

namespace App\Http\Controllers;

use App\User;
use Illuminate\Http\Request;

class Day9 extends Controller
{
    /**
     * 用户注册
     * @param Request $request
     * @return array
     */
    public function LoginAdd(Request $request)
    {
        $name = $request['name'];
        $pwd = md5($request['pwd']);

        $model = new User();
        $data = $model->LoginAdd($name,$pwd);

        if($data)
        {
            return ['code'=>0,'msg'=>'注册成功','res'=>$data];
        }else{
            return ['code'=>1,'msg'=>'注册失败','res'=>null];
        }
    }

    public function Login(Request $request)
    {
        $name = $request['name'];
        $pwd = md5($request['pwd']);

        if($name=='')
        {
            return ['code'=>3,'msg'=>'用户名不能为空','res'=>null];
        }

        $model = new User();
        $res = $model->Login_Do($name);

        if($res['pwd']!=$pwd)
        {
            return ['code'=>2,'msg'=>'用户名或密码错误','res'=>null];
        }
        if($res)
        {
            return ['code'=>0,'msg'=>'登录成功','res'=>$res];
        }else{
            return ['code'=>1,'msg'=>'登录失败','res'=>null];
        }
    }
}
